

let url = ["/admin/Index","/admin/system/menu/list/view",
    "/admin/system/menu/edit/view",
    "/admin/system/menu/add/view",
    "/admin/system/menu/tree/view",
    "/admin/system/menu/icon/view",
    "/admin/system/user/add/view",
    "/admin/system/role/add/view",
    "/admin/system/role/edit/view",
    "/admin/system/user/edit/view",
    "/admin/v1/api/logout",
    "/admin/system/dicttype/add/view",
    "/admin/system/dicttype/edit/view",
    "/admin/system/dictdata/list/view",
    "/admin/system/dictdata/add/view",
    "/admin/system/dictdata/edit/view"
]

const ResultJson = require("../../../../../../ruoling-model/src/com/ruoling/model/SystemModel/ResultJson.js").V("1.0.0");
const Conversation = require("../../../../../../ruoling-common/src/com/ruoling/common/core/conversation.js");

const Authorization = function (Request,response,next){

    let fillter = new Fillter(Request,response,next)

    fillter.exclusive("/admin/login")
        .exclusive("/admin/v1/api/captchaImage")
        .exclusive("/admin/v1/api/login")

    for(let i = 0;i<url.length;i++){
        fillter.SessionExclusive(url[i]);
    }

    fillter.intercept();

};


class Fillter{
    constructor(req,res,next) {
        this.exclusive_Arr=[];
        this.exclusiveSessionArr = [];
        this.req = req;
        this.res = res;
        this.next = next;
    };
    exclusive(str){
        if(str != ""){
            this.exclusive_Arr.push(str);
            return this;
        }
    };

    //通用接口
    SessionExclusive(str){
        if(str != ""){
            this.exclusiveSessionArr.push(str);
            return this;
        }
    };
    async intercept(){
        let usersession = this.req.session.UserSession;
        let MenuSession = this.req.session.MenuSession;
        let WebPath = this.req.originalUrl;

        //首先排除不需要的URL链接
        let exclusiveflog = this.exclusiveUrl(this.exclusive_Arr,WebPath);

        if(exclusiveflog){
            this.next();
            return;
        }else{

            if(usersession == null){

                this.res.send("<script>alert('重新登录');parent.window.location.href='/admin/login'</script>")
              // this.res.redirect("/admin/login")
            }else{
                //登录了，根据角色来决定是否有权限
                let Status_flog = await Conversation.Converintercept(this.req,this.res,this.next);

                if(!Status_flog){
                    this.req.session.MenuSession = null;
                    this.req.session.UserSession = null;
                    this.req.session.captchaImage = null;
                    this.res.send("<script>alert('重新登录');parent.window.location.href='/admin/login'</script>")
                    return;
                }
                let exclusiveSessionflog = this.exclusiveUrl(this.exclusiveSessionArr,WebPath);

                    if(exclusiveSessionflog){
                        this.next();
                        return;
                    }else {

                        let exclusivePermissionflog = this.exclusivePermission(MenuSession,WebPath);

                        if(exclusivePermissionflog){
                            this.next();
                            return;
                        }else{
                            this.res.send(ResultJson.AuthorityError());
                            return;
                        }
                    }

            }
        }

    };

    /**
     * 过滤URL与过滤登录之后的URL
     * @param arr
     * @param webpath
     * @returns {boolean}
     */
    exclusiveUrl(arr,webpath){
        let WebPath = null;
        if(webpath.indexOf("?") != -1){
             WebPath = webpath.substring(0,webpath.indexOf("?"));
        }else{
             WebPath = webpath;
        }


        for(let i = 0;i<arr.length;i++){
            if(arr[i].toUpperCase() == WebPath.toUpperCase()){
                return true;
                break;
            }
        }
    };

    /**
     * 权限判断
     * @param arr
     * @param webpath
     * @returns {boolean}
     */
    exclusivePermission(arr,webpath){
        let WebPath = null;
        if(webpath.indexOf("?") != -1){
             WebPath = webpath.substring(0,webpath.indexOf("?"));
        }else{
             WebPath = webpath;
        }

        for(let i = 0;i<arr.length;i++){

            if(arr[i].sys_permission_url.toUpperCase() == WebPath.toUpperCase()){
                return true;
                break;
            }
        }
    }
}

module.exports = Authorization;



